<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 星云微讯科技有限公司
 * 后台-分类管理
 *  category.php
 *  lichaoliang  2015-03-13
 */

class Category extends Admin_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('category_model');
    }

    /**
     *  分类列表
    */
    public function index(){
        $list = $this->category_model->getAll();
        $this->load->view('admin/category/index.php',array('list'=>$list));
    }

    /**
     *  添加
    */
    public function add(){
        $this->load->view('admin/category/add.php');
    }

    /**
     *  添加逻辑
    */
    public function process_add(){
        header("Content-type:text/html;charset=utf-8");
        $view['title'] = !empty($_POST['title'])?addslashes($_POST['title']):'';
        $view['explain'] = !empty($_POST['explain'])?addslashes($_POST['explain']):0;

        $view['createtime'] = time();


        /* 图片上传 logo */
        $path  = str_replace("\\", "/", FCPATH);
        $config['upload_path'] =$path  . 'uploads/category/';

        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2048';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';
        $config['encrypt_name'] = true;
        $config['remove_spaces'] = true;

        $this->load->library('upload', $config);
        $field_name = "picture"; // 表单名称  <?php echo form_open_multipart('upload/do_upload'); <input type="file" name="field_name" size="20" />


        if ( ! $this->upload->do_upload($field_name))
        {
            $message = $this->upload->display_errors();
            $url_suf = 'admin/category/add';
        }
        else
        {
            $upload_data = $this->upload->data();
            $view['picture'] = 'uploads/category/'.$upload_data['file_name'];

            $id = $this->comm_model->insert('category',$view);
            $message = !empty($id) ? '添加成功' : '添加失败';
            $url_suf = 'admin/category/index';
        }

        alert_and_redirect($message, $url_suf); //alert提示处理结果并跳转到指定地址
    }

    /**
     *  修改
    */
    public function update($id){
        $view = $this->comm_model->get_one($id,'category');
        $this->load->view('admin/category/update.php',array('view'=>$view));
    }

    public function process_update(){
        header("Content-type:text/html;charset=utf-8");
        $id = !empty($_POST['id'])?intval($_POST['id']):0;
        $view['title'] = !empty($_POST['title'])?addslashes($_POST['title']):'';
        $view['explain'] = !empty($_POST['explain'])?addslashes($_POST['explain']):0;
        $field_name = "picture";
        if(!empty($_FILES[$field_name]['tmp_name'])) {
            /* 图片上传 logo */
            $path = str_replace("\\", "/", FCPATH);
            $config['upload_path'] = $path . 'uploads/category/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2048';
            $config['max_width'] = '1024';
            $config['max_height'] = '768';
            $config['encrypt_name'] = true;
            $config['remove_spaces'] = true;

            $this->load->library('upload', $config);
            // 表单名称  <?php echo form_open_multipart('upload/do_upload'); <input type="file" name="field_name" size="20" />


            if (!$this->upload->do_upload($field_name)) {
                $error = $this->upload->display_errors();
                alert_and_redirect($error, 'admin/category/update');
            } else {
                $upload_data = $this->upload->data();
                $view['picture'] = 'uploads/category/'.$upload_data['file_name'];
            }
        }

        $rs = $this->comm_model->update($id,$view,'category');
        alert_and_redirect(!empty($rs) ? '修改成功' : '修改失败', 'admin/category/index');
    }

    // 删除
    public function delete($id){
        header("Content-type:text/html;charset=utf-8");
        $rs = $this->comm_model->del($id,'category');
        alert_and_redirect(!empty($rs) ? '删除成功' : '删除失败', 'admin/category/index');
    }
}
